package com.roamer.school.service.model.sys;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.roamer.school.common.validator.annotation.EnumValue;
import com.roamer.school.common.validator.group.AddGroup;
import com.roamer.school.common.validator.group.UpdateGroup;
import com.roamer.school.repository.entity.sys.SysUser;
import com.roamer.school.repository.enums.UserStatusEnum;
import com.roamer.school.service.converter.sys.UserConverterMapper;
import lombok.Data;
import lombok.experimental.Accessors;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.Serializable;

/**
 * 用户DTO
 *
 * @author roamer
 * @version V1.0
 * @date 2019/1/18 16:13
 */
@Data
@Accessors(chain = true)
public class UserDTO implements Serializable {

    private static final long serialVersionUID = -6225665392539473603L;

    private Long id;

    @NotBlank(message = "用户名为空", groups = {AddGroup.class, UpdateGroup.class})
    @Size(min = 5, max = 15, message = "用户名长度小于5或大于15", groups = {AddGroup.class, UpdateGroup.class})
    private String username;

    @NotBlank(message = "密码为空", groups = {AddGroup.class, UpdateGroup.class})
    @Size(min = 6, max = 30, message = "密码长度小于6或大于30", groups = {AddGroup.class, UpdateGroup.class})
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String password;

    private String nickname;

    @EnumValue(enumClass = UserStatusEnum.class, enumMethod = "isValidName")
    private Integer status;

    public SysUser reverseConvert() {
        return UserConverterMapper.MAPPER.reverseConvert(this);
    }

    public static UserDTO convertFor(SysUser user) {
        return UserConverterMapper.MAPPER.convert(user);
    }

}
